package com.maiji.cloud.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.maiji.cloud.entities.shopingmall.CapitalMain;
import com.maiji.cloud.entities.shopingmall.DistributionComRuleLog;
import com.maiji.cloud.entities.shopingmall.DistributionLogEntity;
import com.maiji.cloud.request.ReqMetaData;
import com.maiji.cloud.request.shopingmall.DbApplyExamineReqData;
import com.maiji.cloud.request.shopingmall.DbApplyInfoReqData;
import com.maiji.cloud.response.BaseMetaResDto;
import com.maiji.cloud.response.Status;

import com.maiji.cloud.service.DistributionLogService;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
import com.maiji.cloud.entities.shopingmall.DistributionFundRuleEntity;
import com.maiji.cloud.request.BaseDataReqDto;
import com.maiji.cloud.request.shopingmall.DistributionLogListReqDto;
import com.maiji.cloud.request.shopingmall.SubmitApplayReqData;
import com.maiji.cloud.response.BaseDataResDto;
import com.maiji.cloud.response.BaseResDto;
import com.maiji.cloud.response.shopingmall.DistributionAgentApplyInfoResData;
import com.maiji.cloud.response.shopingmall.DistributionLogListResDto;
import com.maiji.cloud.service.DistributionService;

@RestController
@RequestMapping("/distribution/")
public class DistributionController {

	Logger logger = LoggerFactory.getLogger(DistributionController.class);
	@Autowired
	private DistributionService distributionService;
	@Autowired
	private DistributionLogService distributionLogService;

	/**
	 * 用户申请分销代理
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("submitApplay")
	public BaseResDto submitApplay(@RequestBody BaseDataReqDto<SubmitApplayReqData> param,@RequestHeader String maijiToken) {
		logger.info("DistributionController.submitApplay ,param is {},maijiToken is {}",param,maijiToken);
		if(StringUtils.isBlank(param.getData().getName()) || StringUtils.isBlank(param.getData().getPhone())||StringUtils.isBlank(param.getData().getWxNum()))
			return new BaseResDto(Status.ERROR,"参数为空");
		return distributionService.submitApplay(param,maijiToken);
	}

	/**
	 * 获取一、二级用户的佣金比例
	 * @return
	 */
	@PostMapping("getCommissionRate")
	public BaseDataResDto<List<DistributionFundRuleEntity>> getCommissionRate(){
		
		return distributionService.getCommissionRate();
	}

	/**
	 * 分销申请信息
	 * @return
	 */
	@PostMapping("getDistributionAgentApplyInfo")
	public BaseDataResDto<DistributionAgentApplyInfoResData> getDistributionAgentApplyInfo(@RequestHeader String maijiToken){
		return distributionService.getDistributionAgentApplyInfo(maijiToken);
	}

	/**
	 * 获取分销申请信息列表
	 * @return
	 */
	@PostMapping("findAllDbApplyInfos")
	public BaseMetaResDto<List<DistributionAgentApplyInfoResData>> findAllDbApplyInfos(@RequestBody BaseDataReqDto<DbApplyInfoReqData> baseDataReqDto){
        DbApplyInfoReqData data = baseDataReqDto.getData();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        return distributionService.findAllDbApplyInfos(data, metaData);
	}

	/**
	 * 审核分销申请
	 * @return
	 */
	@PostMapping("examineDbApply")
	public BaseResDto examineDbApply(@RequestBody BaseDataReqDto<DbApplyExamineReqData> baseDataReqDto){
        DbApplyExamineReqData data = baseDataReqDto.getData();
        return distributionService.examineDbApply(data);
	}
	
	/**
	 * 获得分销总额
	 * @param param
	 * @param maijiToken
	 * @return
	 */
	@PostMapping("getDistributionSumMoney")
	public Double getDistributionSumMoney(@RequestBody DistributionLogListReqDto param, @RequestHeader(name = "maijiToken") String maijiToken) {
		logger.info("CapitalController.getDistributionSumMoney,maijiToken is {},param is {}", maijiToken,JSON.toJSONString(param));
		return distributionService.getDistributionSumMoney(param,maijiToken);
	}
	
	/**
	 * 分销明细列表
	 * @param maijiToken
	 */
	@PostMapping("getDistributionLogList")
	public DistributionLogListResDto getDistributionLogList(@RequestBody DistributionLogListReqDto param,@RequestHeader("maijiToken") String maijiToken) {
		logger.info("CapitalController.getDistributionLogList,maijiToken is {},param is {}", maijiToken,JSON.toJSONString(param));
		
		return distributionService.getDistributionLogList(param,maijiToken);
		
	}

    /**
     * 获取分销流水列表
     */
    @PostMapping("findAllDistributionLogs")
    public BaseMetaResDto<List<DistributionLogEntity>> findAllDistributionLogs (@RequestBody BaseDataReqDto<DistributionLogEntity> baseDataReqDto) {
        String userId = baseDataReqDto.getData().getUserId();
        String orderNo = baseDataReqDto.getData().getOrderNo();
        String nickName = baseDataReqDto.getData().getNickName();
        List<Date> inTimes = baseDataReqDto.getData().getInTimes();
        ReqMetaData metaData = baseDataReqDto.getMetaData();
        return distributionLogService.findAllDistributionLogs(userId, nickName, orderNo, inTimes, metaData);
    }

    /**
     * 获取用户的账户信息
     */
    @PostMapping("findCapitalMainByUserId")
    public BaseDataResDto<CapitalMain> findCapitalMainByUserId (@RequestBody BaseDataReqDto<String> baseDataReqDto) {
        String userId = baseDataReqDto.getData();
        return distributionLogService.findCapitalMainByUserId(userId);
    }

    /**
     * 获取分销公共规则
     */
    @PostMapping("findDistributionComRule")
    public BaseDataResDto<DistributionComRuleLog> findDistributionComRule () {
        return distributionLogService.findDistributionComRule();
    }

    /**
     * 获取分销公共规则修改记录列表
     */
    @PostMapping("findAllDistributionComRuleLogs")
    public BaseDataResDto<List<DistributionComRuleLog>> findAllDistributionComRuleLogs(@RequestBody BaseDataReqDto<List<Date>> baseDataReqDto) {
        List<Date> editDates = baseDataReqDto.getData();
        return distributionLogService.findAllDistributionComRuleLogs(editDates);
    }

    /**
     * 修改分销公共规则
     */
    @PostMapping("editDistributionComRule")
    public BaseResDto editDistributionComRule(@RequestBody BaseDataReqDto<DistributionComRuleLog> baseDataReqDto) {
        DistributionComRuleLog distributionComRuleLog = baseDataReqDto.getData();
        return distributionLogService.editDistributionComRule(distributionComRuleLog);
    }


    /**
     * 获取分销资金统计
     * @return
     */
    @PostMapping("findDistributionCapitalCount")
    public BaseDataResDto<Map<String, Double>> findDistributionCapitalCount () {
        return distributionService.findDistributionCapitalCount();
    }

}
